//
//  NSString+DDPath.m
//  IOSDuoduo
//
//  Created by abc on 14-6-3.
//  Copyright (c) 2014年 dujia. All rights reserved.
//

#import "NSString+DDPath.h"

@implementation NSString (DDPath)

+ (NSString*)userDocmentDirection
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    return documentsDirectory;
}

+ (NSString*)userExclusiveDirectionWithKey:(NSString *)key
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString* directorPath = [documentsDirectory stringByAppendingPathComponent:key];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:directorPath])
    {
        [fileManager createDirectoryAtPath:directorPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return directorPath;
}

+ (NSString *)imageUrlFromThumbnailUrl:(NSString *)thumbnailUrl
{
    
    NSRange range = [thumbnailUrl rangeOfString:@"_s" options:NSBackwardsSearch | NSCaseInsensitiveSearch];
    if (range.length == 0) {
        return thumbnailUrl;
    }
    
    NSString *url = [thumbnailUrl stringByReplacingCharactersInRange:range withString:@""];
    return url;
    
    
//    NSString *patternString = @"\\d+x\\d+_s";
//    NSError *error;
//    NSRegularExpression *pattern = [[NSRegularExpression alloc] initWithPattern:patternString options:NSRegularExpressionCaseInsensitive error:&error];
//    
//    if (error) {
//        NSLog(@"Create regex pattern:%@ error:%@",patternString, error);
//    }
//    
//    NSRange range = NSMakeRange(0, thumbnailUrl.length);
//    
//    NSString *url = [pattern stringByReplacingMatchesInString:thumbnailUrl options:0 range:range withTemplate:@""];
//    
//    return url;
}

+ (NSString *)thumbnailUrlFormImageUrl:(NSString *)imageUrl
{
    if ([imageUrl hasSuffix:@"send"]) {
        return imageUrl;
    }
    
    NSRange dotRange = [imageUrl rangeOfString:@"." options:NSBackwardsSearch];
    if (dotRange.length == 0) {
        return imageUrl;
    }
    
    NSString *thumbnailUrl =  [imageUrl stringByReplacingCharactersInRange:dotRange withString:@"_s."];
    
    return thumbnailUrl;
}

@end
